/**
 * 
 */
package steffens.java.gui.defaultui.interactive.buttons.eventbased;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;

import steffens.java.gui.defaultui.interactive.DataGrabber;
import steffens.java.gui.defaultui.interactive.events.buttons.ConfirmEvent;
import steffens.java.gui.defaultui.interactive.listener.ButtonEventListener;

/**
 * @author Steffen Baumann
 * @version 1.0 
 *
 */
public class ConfirmButton extends DefaultEventButton implements ActionListener{

	/** */
	private static final long serialVersionUID = 1L;
	private DataGrabber dataGrabber = null;
	/**
	 * 
	 */
	public ConfirmButton() {
		this("OK", DEFAULT_ICON, DEFAULT_SIZE);
	}

	/**
	 * @param text
	 */
	public ConfirmButton(String text) {
		this(text, DEFAULT_ICON, DEFAULT_SIZE);
	}
	
	/**
	 * @param icon
	 */
	public ConfirmButton(Icon icon) {
		this(DEFAULT_TEXT, icon, DEFAULT_SIZE);
	}
	
	/**
	 * @param size
	 */
	public ConfirmButton(Dimension size) {
		this(DEFAULT_TEXT, DEFAULT_ICON, size);
	}
	
	/**
	 * @param text
	 * @param icon
	 * @param size
	 */
	public ConfirmButton(String text, Icon icon, Dimension size) {
		super(text, icon, size);
		this.addActionListener(this);
//		setDataGrabber(null);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(this)){
			for(ButtonEventListener listener : getButtonEventListeners()){
				listener.firedEvent(new ConfirmEvent(dataGrabber.grabDataFromElement()));	
			}
		}
	}
	
	public DataGrabber getDataGrabber(){
		return this.dataGrabber;
	}
	
	public void setDataGrabber(DataGrabber dataGrabber){
		if(dataGrabber == null){
			this.dataGrabber = new DataGrabber() {
				
				@Override
				public Object grabDataFromElement() {
					return null;
				}
			};
		}else{
			this.dataGrabber = dataGrabber;
		}
		
	}
}
